package com.ship.boats.service.impl;

import com.ship.boats.mapper.SpBoatsAisMessageMapper;
import com.ship.boats.service.SpBoatsAisMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <b>TestServiceImpl</b>
 *
 * @description: TestServiceImpl <br>
 * @date: 2024/4/11 16:44 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class TestServiceImpl {

    @Autowired
    private SpBoatsAisMessageMapper spBoatsAisMessageMapper;

    @Autowired
    private SpBoatsAisMessageService spBoatsAisMessageService;


//    @Scheduled(fixedRate = 120000) // 每分钟检查一次
    public void scheduleCreateTable() {
        // 获取当前时间，根据需要可以自定义时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String tableSuffix = sdf.format(new Date());
        String tableName = "boats_ais_message" + tableSuffix;
        String status = spBoatsAisMessageMapper.queryTableName(tableName);
        if ("f".equals(status)){
            spBoatsAisMessageMapper.createTable(tableName);
            System.out.println("创建新表结束");
        }
        spBoatsAisMessageService.addBoatsAirMessage();


//        try (Connection connection = dataSource.getConnection()) {
//            // 检查表是否存在
//            try (Statement statement = connection.createStatement()) {
//                String checkTableSql = "SELECT to_regclass('\"" + tableName + "\"')";
//                if (statement.executeQuery(checkTableSql).next()) {
//                    // 表已存在
//                    return;
//                }
//            }

            // 创建新表的SQL语句，这里需要根据实际表结构来定义
//            String createTableSql = "CREATE TABLE \"" + tableName + "\" (LIKE your_original_table INCLUDING ALL)";
//            try (Statement statement = connection.createStatement()) {
//                statement.execute(createTableSql);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
